package com.anysoft.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.UserBean;
import com.anysoft.utils.ParamUtil;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.ArrayList;

public class UserManageAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<UserBean> mList;

    private static final int NORMAL_TYPE = 0;
    private static final int ADD_TYPE = 1;

    public UserManageAdapter(Context context, ArrayList<UserBean> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            if (position < mList.size()) {
                return NORMAL_TYPE;
            } else {
                return ADD_TYPE;
            }
        } else {
            return ADD_TYPE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NORMAL_TYPE) {
            return new UserManageViewHolder(mInflater.inflate(R.layout.recyclerview_item_user_manage, parent, false));
        } else {
            return new UserManageAddViewHolder(mInflater.inflate(R.layout.recyclerview_item_user_manage_add, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof UserManageViewHolder) {
            final UserManageViewHolder viewHolder = (UserManageViewHolder) holder;

            String pathUrl = mList.get(position).getUserImgPath();
            if (!TextUtils.isEmpty(pathUrl)) {
                Logger.t("pathUrl").d(pathUrl);
                Bitmap bitmap = getDiskBitmap(pathUrl);
                if (bitmap != null) {
                    viewHolder.iv_item_user_manage.setImageBitmap(bitmap);
                }
            } else {
                viewHolder.iv_item_user_manage.setBackground(mContext.getResources().getDrawable(R.mipmap.icon_user));
            }

            viewHolder.tv_item_user_manage_name.setText(ParamUtil.formatUserName(mList.get(position).getUserName()));
//            System.out.println("mList.get(position).getUserName()---" + ParamUtil.formatUserName(mList.get(position).getUserName()));
            if (!((BaseActivity) mContext).mentorApplication.needLogin()) {
                if (((BaseActivity) mContext).mentorApplication.userBean.getUserId().equals(mList.get(position).getUserId())) {
                    viewHolder.iv_item_user_manage_selected.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.iv_item_user_manage_selected.setVisibility(View.INVISIBLE);
                }
            } else {
                viewHolder.iv_item_user_manage_selected.setVisibility(View.INVISIBLE);
            }
        } else {
            final UserManageAddViewHolder viewHolder = (UserManageAddViewHolder) holder;
        }
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() + 1 : 1;
    }

    public void removeItem(int position) {
        mList.remove(position);
//        notifyItemRemoved(position);
        if (position != mList.size()) {
            notifyItemRemoved(position);
        }
//        notifyDataSetChanged();
    }

    private Bitmap getDiskBitmap(String pathString) {
        Bitmap bitmap = null;
        try {
            File file = new File(pathString);
            if (file.exists()) {
                bitmap = BitmapFactory.decodeFile(pathString);
                //System.out.println("bitmap存在");
            }
        } catch (Exception e) {
            //System.out.println("bitmap不存在");
        }
        return bitmap;
    }
}
